package com.example.wangning.mycarofcountry.recommend.evaluating;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.toolbox.ImageLoader;
import com.example.mycarofcountry.entity.dao.Recommend_Evaluating;
import com.example.wangning.mycarofcountry.R;
import com.example.wangning.mycarofcountry.cache.VolleySingleton;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by dllo on 16/1/12.
 */
public class EvaluatingAdapter extends RecyclerView.Adapter<EvaluatingAdapter.EvaluatingViewHolder>{
    private List<Recommend_Evaluating> evaluatings;
    private VolleySingleton queue;
    private ImageLoader imageLoader;
    public EvaluatingAdapter(Context context) {
        queue = VolleySingleton.getVolleyIntance();
        imageLoader = queue.getImageLoader();
        evaluatings = new ArrayList<>();
    }
    public void addRefreshData(List<Recommend_Evaluating> t) {
        evaluatings = t;
        notifyDataSetChanged();
    }

    public void addLoadData(List<Recommend_Evaluating> t) {
        evaluatings.addAll(t);
        notifyDataSetChanged();
    }


    @Override
    public EvaluatingViewHolder onCreateViewHolder(ViewGroup viewGroup, int position) {
        LayoutInflater inflater = LayoutInflater.from(viewGroup.getContext());
        View view = inflater.inflate(R.layout.recycler_evaluating_item,null);
        return new EvaluatingViewHolder(view);
    }

    @Override
    public void onBindViewHolder(EvaluatingViewHolder holder, int position) {
        if (evaluatings != null && evaluatings.size() > 0){
            Recommend_Evaluating evaluating = evaluatings.get(position);
            if (evaluating != null){
                holder.titleTv.setText(evaluating.getTitle());
                if (evaluating.getSmallpic() != null && evaluating.getSmallpic().length() > 0){
                    String imgUrl = evaluating.getSmallpic();
                    queue.getBitmap(queue,imgUrl, holder.imageView);
                }
            }
        }

    }

    @Override
    public int getItemCount() {
        return evaluatings != null ? evaluatings.size() : 0;
    }

    class EvaluatingViewHolder extends RecyclerView.ViewHolder{
        TextView titleTv;
        ImageView imageView;
        public EvaluatingViewHolder(View itemView) {
            super(itemView);
            titleTv = (TextView) itemView.findViewById(R.id.titleTv);
            imageView = (ImageView) itemView.findViewById(R.id.imageView);
        }
    }
}
